﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies
{
    public partial class Upload : Form
    {
        private int projectID;
        private string ftp = "ftp://ftp.qq-design.com";
        private string usn = "alex";
        private string pass = "Huibenalexmoetengoedophunwachtwoordletten";
        private MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
        private string pathname;
        private string ftpfilepath;

        public Upload()
        {
            
        }

        public Upload(int projectid)
        {
            projectID = projectid;
            InitializeComponent();
        }

        private void uploadFile(string FTPAddress, string filePath)
        {
            //Create FTP request
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress + "/" + projectID + "/" + Path.GetFileName(filePath)));

            //pathnaam ophalen om in db te stoppen
            pathname = Path.GetFileName(filePath);
            ftpfilepath = ftp+'/'+projectID+'/'+pathname;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(usn, pass);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
            
            //Load the file
            FileStream stream = File.OpenRead(filePath);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            //Upload file
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            //filepath uploaden naar database om zo downloads te regelen
            try
            {
                string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                MySqlCommand filepathUpload = new MySqlCommand("INSERT INTO doc_upload (DocID, Docname, DocPath, ProjectID, Entrydate)" + "VALUES ('','"+pathname+"', '"+ftpfilepath+"', '"+projectID+"', '"+datetime+"')", grabConnection);
                grabConnection.Open();
                filepathUpload.ExecuteNonQuery();
            }
            catch(MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Dispose();
            }

            MessageBox.Show("Uploaded Successfully");
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            Application.DoEvents();

            uploadFile(ftp, txtFilePath.Text);
            btnUpload.Enabled = true;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = openFile.FileName;
            }
        }
    }
}
